package com.xc.file.web.controller;

import com.xc.file.api.NoticeApi;
import com.xc.file.bean.DownloadBean;
import com.xc.file.config.Constants;
import com.xc.file.dto.DownloadDto;
import com.xc.file.enums.FailCode;
import com.xc.file.info.FolderInfo;
import com.xc.file.service.DownloadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;

/**
 * <p>项目名称: Stars</p>
 * <p>描述: 下载文件请求接口</p>
 * <P>创建人:小程 </p>
 * <P>创建人邮箱: 779943476@qq.com</p>
 * <p>创建时间: 2019/8/27</p>
 *
 * @version v1.0
 */
@Controller
@Slf4j
public class DownloadRest {

    @Autowired
    private DownloadService downloadService;

    @Autowired
    private Constants constants;
    @Autowired
    private NoticeApi noticeApi;

    /**
     * 根据文件信息下载文件,接口文档只支持下载本地文件
     *
     * @param hashCode 文件hashCode
     * @param fid      文件id
     * @param response 响应response
     * @param request  请求request
     * @param open     是否直接打开
     */
    @GetMapping("/file/download/{hashCode}")
    public void downloadFile(@PathVariable("hashCode") String hashCode,
                             @RequestParam(value = "fid", required = false) String fid,
                             @RequestParam(value = "open", required = false) Integer open,
                             HttpServletResponse response, HttpServletRequest request) {

        String fileName;
        if (constants.getOpenNotice()) {
            if (hashCode.contains(".")) {
                hashCode = hashCode.substring(0, hashCode.lastIndexOf("."));
            }
            DownloadDto downloadDto = constants.getFileDownloadMap().get(fid + hashCode);
            if (downloadDto == null) {
                DownloadBean downloadBean = new DownloadBean();
                downloadBean.setAppId(constants.getAppId());
                downloadBean.setAppSecret(constants.getAppSecret());
                downloadBean.setFid(fid);
                //验证文件
                downloadDto = noticeApi.noticeDownload(downloadBean);
                if (downloadDto == null) {
                    throw FailCode.NOT_HAVE_AUTHORITY.getOperateException();
                }
                if (!hashCode.equals(downloadDto.getHashCode())) {
                    throw FailCode.HASH_ERROR.getOperateException();
                }
                fileName = downloadDto.getName();
                constants.getFileDownloadMap().put(fid + hashCode, downloadDto);
            } else {
                fileName = downloadDto.getName();
            }
        } else {
            String name = "";
            if (hashCode.contains(".")) {
                name = hashCode.substring(hashCode.lastIndexOf("."), hashCode.length());
                hashCode = hashCode.substring(0, hashCode.lastIndexOf("."));
            }
            fileName = hashCode + name;
        }
        if (open == null) {
            open = 1;
        }
        downloadService.downloadFile(fileName, hashCode, request.getHeader("range"), response, open);
    }
}
